/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/callback',
    component: () => import('@/views/callback'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index'),
        meta: {
          title: '首页',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/equipment',
    name: 'Equipment',
    component: Layout,
    meta: {
      title: '设备',
      icon: 'door-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'line',
        name: 'Line',
        component: () => import('@/views/equipment/line.vue'),
        meta: {
          title: '产线管理',
          icon: 'equalizer-line',
        },
      },
      {
        path: 'equipment',
        name: 'Equipment',
        component: () => import('@/views/equipment/index.vue'),
        meta: {
          title: '设备台账',
          icon: 'focus-line',
        },
      },
      {
        path: 'maintain',
        name: 'Maintain',
        meta: {
          title: '设备保养',
          icon: 'file-settings-line',
        },
        children: [
          {
            path: 'maintainPlan',
            name: 'MaintainPlan',
            component: () => import('@/views/equipment/maintain/plan.vue'),
            meta: {
              title: '设备保养方案',
            },
          },
          {
            path: 'maintainIndex',
            name: 'MaintainIndex',
            component: () => import('@/views/equipment/maintain/index.vue'),
            meta: {
              title: '保养信息',
            },
          },
        ],
      },
      {
        path: 'repair',
        name: 'Repair',
        meta: {
          title: '设备维修',
          icon: 'file-text-line',
        },
        children: [
          {
            path: 'repairPlan',
            name: 'RepairPlan',
            component: () => import('@/views/equipment/repair/plan.vue'),
            meta: {
              title: '设备报修单',
            },
          },
          {
            path: 'repairIndex',
            name: 'RepairIndex',
            component: () => import('@/views/equipment/repair/index.vue'),
            meta: {
              title: '维修信息',
            },
          },
        ],
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/views/equipment/statistics.vue'),
        meta: {
          title: '使用统计',
          icon: 'file-paper-2-line',
        },
      },
      {
        path: 'monitoring',
        name: 'Monitoring',
        component: () => import('@/views/equipment/monitoring.vue'),
        meta: {
          title: '状态监控',
          icon: 'computer-line',
        },
      },
      {
        path: 'malfunction',
        name: 'Malfunction',
        component: () => import('@/views/equipment/malfunction.vue'),
        meta: {
          title: '故障管理',
          icon: 'settings-3-line',
        },
      },
    ],
  },
  {
    path: '/product',
    name: 'Product',
    component: Layout,
    meta: {
      title: '产品',
      icon: 'product-hunt-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'product',
        name: 'Product',
        component: () => import('@/views/product/index.vue'),
        meta: {
          title: '产品信息',
          icon: 'list-check-2',
        },
      },
      {
        path: 'technology',
        name: 'Technology',
        component: () => import('@/views/product/technology.vue'),
        meta: {
          title: '工艺管理',
          icon: 'asterisk',
        },
      },
      {
        path: 'procedure',
        name: 'Procedure',
        component: () => import('@/views/product/procedure.vue'),
        meta: {
          title: '程序管理',
          icon: 'projector-line',
        },
      },
      {
        path: 'bom',
        name: 'Bom',
        component: () => import('@/views/product/bom.vue'),
        meta: {
          title: 'BOM管理',
          icon: 'bold',
        },
      },
    ],
  },
  {
    path: '/material',
    name: 'Material',
    component: Layout,
    meta: {
      title: '物料',
      icon: 'apps-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'workblank',
        name: 'Workblank',
        // component: () => import('@/views/material/workblank.vue'),
        meta: {
          title: '毛坯料管理',
          icon: 'arrow-up-down-line',
        },
        children: [
          {
            path: 'workblankIndex',
            name: 'WorkblankIndex',
            component: () => import('@/views/material/workblank/index.vue'),
            meta: {
              title: '物料信息',
            },
          },
          {
            path: 'workblankType',
            name: 'WorkblankType',
            component: () => import('@/views/material/workblank/type.vue'),
            meta: {
              title: '物料类型',
            },
          },
          {
            path: 'workblankSupplier',
            name: 'WorkblankSupplier',
            component: () => import('@/views/material/workblank/supplier.vue'),
            meta: {
              title: '供应商',
            },
          },
        ],
      },
      {
        path: 'tray',
        name: 'Tray',
        meta: {
          title: '托盘管理',
          icon: 'todo-line',
        },
        children: [
          {
            path: 'trayIndex',
            name: 'TrayIndex',
            component: () => import('@/views/material/tray/index.vue'),
            meta: {
              title: '托盘信息',
            },
          },
          {
            path: 'trayType',
            name: 'TrayType',
            component: () => import('@/views/material/tray/type.vue'),
            meta: {
              title: '托盘种类',
            },
          },
          {
            path: 'traySupplier',
            name: 'TraySupplier',
            component: () => import('@/views/material/tray/supplier.vue'),
            meta: {
              title: '供应商',
            },
          },
        ],
      },
      {
        path: 'rack',
        name: 'Rack',
        component: () => import('@/views/material/rack.vue'),
        meta: {
          title: '料架信息',
          icon: 'repeat-line',
        },
      },
      {
        path: 'tool',
        name: 'Tool',
        component: () => import('@/views/material/tool.vue'),
        meta: {
          title: '刀具管理',
          icon: 'repeat-line',
        },
      },
    ],
  },
  {
    path: '/production',
    name: 'production',
    component: Layout,
    meta: {
      title: '计划生产',
      icon: 'drag-move-2-fill',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'pmonitoring',
        name: 'Pmonitoring',
        component: () => import('@/views/production/monitoring.vue'),
        meta: {
          title: '生产监控',
          icon: 'tv-2-line',
        },
      },
      {
        path: 'porder',
        name: 'Porder',
        component: () => import('@/views/production/order.vue'),
        meta: {
          title: '生产订单',
          icon: 'list-unordered',
        },
      },
      {
        path: 'pplan',
        name: 'Pplan',
        component: () => import('@/views/production/plan.vue'),
        meta: {
          title: '生产计划',
          icon: 'order-play-line',
        },
      },
      {
        path: 'work',
        name: 'Work',
        component: () => import('@/views/production/work.vue'),
        meta: {
          title: '工单管理',
          icon: 'file-word-line',
        },
      },
      {
        path: 'workflow',
        name: 'Workflow',
        component: () => import('@/views/workflow/index.vue'),
        meta: {
          hidden: true,
          levelHidden: true,
        },
      },
      {
        path: 'voluntarily',
        name: 'Voluntarily',
        component: () => import('@/views/production/voluntarily.vue'),
        meta: {
          title: '自动排产',
          icon: 'aspect-ratio-line',
        },
      },
    ],
  },
  {
    path: '/quality',
    name: 'Quality',
    component: Layout,
    meta: {
      title: '质量管理',
      icon: 'user-settings-line',
      guard: ['Admin'],
    },
    children: [
      {
        path: 'random',
        name: 'random',
        component: () => import('@/views/quality/random.vue'),
        meta: {
          title: '随机抽检',
          icon: 'map-pin-user-line',
        },
      },
      {
        path: 'retrospect',
        name: 'retrospect',
        component: () => import('@/views/quality/retrospect.vue'),
        meta: {
          title: '质量追溯',
          icon: 'user-3-line',
        },
      },
    ],
  },

  {
    path: '/setting',
    name: 'PersonnelManagement',
    component: Layout,
    meta: {
      title: '配置',
      icon: 'user-settings-line',
      guard: ['Admin'],
    },
    children: [
      {
        path: 'personalCenter',
        name: 'PersonalCenter',
        component: () => import('@/views/setting/personalCenter'),
        meta: {
          title: '个人中心',
          icon: 'map-pin-user-line',
        },
      },
      {
        path: 'userManagement',
        name: 'UserManagement',
        component: () => import('@/views/setting/userManagement'),
        meta: {
          title: '用户管理',
          icon: 'user-3-line',
        },
      },
      {
        path: 'roleManagement',
        name: 'RoleManagement',
        component: () => import('@/views/setting/roleManagement'),
        meta: {
          title: '角色管理',
          icon: 'admin-line',
        },
      },
      {
        path: 'customerManagement',
        name: 'DepartmentManagement',
        component: () => import('@/views/setting/customerManagement'),
        meta: {
          title: '客户管理',
          icon: 'group-line',
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter()

function fatteningRoutes(routes) {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

export function resetRouter(routes = constantRoutes) {
  routes.map((route) => {
    if (route.children) {
      route.children = fatteningRoutes(route.children)
    }
  })
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
